Skip to content

查找绑定窗口图片 - FindWindowsFromPath

函数简介

在绑定窗口的指定区域内查找指定图像,模板通过文件路径指定,支持多图,返回第一个匹配结果。

接口名称

FindWindowsFromPath

DLL调用

long FindWindowsFromPath(long instance, int x1, int y1, int x2, int y2, string templ, string deltaColor, double matchVal, int dir);

参数说明

参数名类型说明
instance长整数型OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。
x1整数型搜索区域左上角X坐标
y1整数型搜索区域左上角Y坐标
x2整数型搜索区域右下角X坐标
y2整数型搜索区域右下角Y坐标
templ字符串模板图片的路径,可以是多个图片,如"test.bmp|test2.bmp|test3.bmp"
deltaColor字符串颜色差值,格式为"RRGGBB",如"101010"
matchVal双精度浮点数相似度,如0.85,最大为1
dir整数型查找方向:0-从左到右从上到下;1-从左到右从下到上;2-从右到左从上到下;3-从右到左从下到上;4-从中心往外查找;5-从上到下从左到右;6-从上到下从右到左;7-从下到上从左到右;8-从下到上从右到左

示例

SDK 调用

cpp
#include "OLAPlugServer.h"

OLAPlugServer ola;
// 0,0,0,0 表示绑定窗口整个客户区
MatchData result = ola.FindWindowsFromPath(0, 0, 0, 0, "img/btn_ok.bmp", "101010", 0.9, 0);
if (result.MatchState) {
    int x = result.X;
    int y = result.Y;
}
csharp
using OLAPlug;

var ola = new OLAPlugServer();
// 0,0,0,0 表示绑定窗口整个客户区
var result = ola.FindWindowsFromPath(0, 0, 0, 0, "img/btn_ok.bmp", "101010", 0.9, 0);
if (result.MatchState)
{
    int x = result.X;
    int y = result.Y;
}
python
from OLAPlugServer import OLAPlugServer

ola = OLAPlugServer()
# 0,0,0,0 表示绑定窗口整个客户区
result = ola.FindWindowsFromPath(0, 0, 0, 0, "img/btn_ok.bmp", "101010", 0.9, 0)
if result.get("MatchState"):
    x = result["X"]
    y = result["Y"]
java
import com.olaplug.OLAPlugServer;
import com.olaplug.model.MatchResult;
import java.util.List;

OLAPlugServer ola = new OLAPlugServer();
MatchResult result = ola.FindWindowsFromPath(0, 0, 0, 0, "img/btn_ok.bmp", "101010", 0.9, 0);
if (result != null && result.MatchState) {
    int x = result.X;
    int y = result.Y;
}
cpp
var ola = com("OlaPlug.OlaSoft")
var result = ola.FindWindowsFromPath(0, 0, 0, 0, "img/btn_ok.bmp", "101010", 0.9, 0)
if(result.MatchState) {
    var x = result.X
}
vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
Set result = ola.FindWindowsFromPath(0, 0, 0, 0, "img/btn_ok.bmp", "101010", 0.9, 0)
If result.MatchState Then
text
.局部变量 ola, OLAPlug
ola.创建 ()
result = ola.FindWindowsFromPath(0, 0, 0, 0, "img/btn_ok.bmp", "101010", 0.9, 0)
.如果真 (result.MatchState)
aardio
import OLAPlugServer;
var ola = OLAPlugServer();
var result = ola.FindWindowsFromPath(0, 0, 0, 0, "img/btn_ok.bmp", "101010", 0.9, 0);
if(result.MatchState){
    var x = result.X;
}
text
变量 ola <类型 = OLAPlugServer>
ola = 新建 OLAPlugServer
MatchData result = ola.FindWindowsFromPath(0, 0, 0, 0, "img/btn_ok.bmp", "101010", 0.9, 0)
如果真 (result.MatchState)
cpp
#include "OLAPlugServer.h"

OLAPlugServer ola;
// 0,0,0,0 表示绑定窗口整个客户区
MatchData result = ola.FindWindowsFromPath(0, 0, 0, 0, "img/btn_ok.bmp", "101010", 0.9, 0);
if (result.MatchState) {
    int32_t x = result.X;
    int32_t y = result.Y;
}

原生 DLL 调用

cpp
long instance = CreateCOLAPlugInterFace();
long ptr = FindWindowsFromPath(instance, 0, 0, 0, 0, "img/btn_ok.bmp", "101010", 0.9, 0);
if (ptr != 0) {
    int matchState = 0, x = 0, y = 0, width = 0, height = 0, index = 0;
    double matchVal = 0, angle = 0;
    char json[4096] = {0};
    GetStringFromPtr(ptr, json, sizeof(json));
    ParseMatchImageJson(instance, json, &matchState, &x, &y, &width, &height, &matchVal, &angle, &index);
    FreeStringPtr(instance, ptr);
}
csharp
using System.Runtime.InteropServices;
using System.Text;

[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int GetStringFromPtr(long ptr, StringBuilder lpString, int size);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int FreeStringPtr(long ptr);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int GetStringSize(long ptr);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long CreateCOLAPlugInterFace();
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long FindWindowsFromPath(long ola, int x1, int y1, int x2, int y2, string templ, string deltaColor, double matchVal, int dir);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int ParseMatchImageJson(string str, out int matchState, out int x, out int y, out int width, out int height, out double matchVal, out double angle, out int index);

long instance = CreateCOLAPlugInterFace();
long ptr = FindWindowsFromPath(instance, 0, 0, 0, 0, "img/btn_ok.bmp", "101010", 0.9, 0);
if (ptr != 0) {
    StringBuilder sb = new StringBuilder(GetStringSize(ptr) + 1);
    GetStringFromPtr(ptr, sb, sb.Capacity);
    FreeStringPtr(ptr);
    ParseMatchImageJson(sb.ToString(), out int matchState, out int x, out int y, out int w, out int h, out double mv, out double ang, out int idx);
}
python
from ctypes import CDLL, c_int, c_int64, create_string_buffer

ola = CDLL("OLAPlug_x64.dll")
ola.CreateCOLAPlugInterFace.restype = c_int64
instance = ola.CreateCOLAPlugInterFace()
ptr = ola.FindWindowsFromPath(instance, ...)
# 原生返回 JSON 字符串指针,可用 ParseMatchImageJson 解析

返回值

字符串指针,返回JSON格式的匹配结果。DLL调用返回字符串指针地址,需要调用 FreeStringPtr 接口释放内存。

返回数据格式:

json
{
    "MatchVal": 0.85,
    "MatchState": true,
    "Index": 0,
    "X": 100,
    "Y": 200,
    "Width": 100,
    "Height": 100
}
字段名类型说明
MatchVal浮点数匹配相似度。
MatchState布尔是否匹配成功。
Index整数结果索引(从 0 开始)。
X整数X 坐标。
Y整数Y 坐标。
Width整数宽度。
Height整数高度。

注意事项

  • x1、y1、x2、y2都传0时,将搜索整个窗口客户区